package com.education.platform.management.controller.user;

import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.CourseCommentDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.CourseCommentService;
import com.education.platform.management.vo.request.CourseCommentQueryVO;
import com.education.platform.management.vo.response.CourseCommentPageInfoVO;
import com.education.platform.management.vo.response.CourseCommentResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* describe: 课程评论
* @author : tomato
* @date: 2020-06-29 10:28:07.323
**/
@RestController
@Api("留言")
@RequestMapping("comment")
@Validated
public class CourseCommentController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CourseCommentController.class);

	@Autowired
    private CourseCommentService courseCommentService;
    /**
     * 分页查询
     * @param courseCommentQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public CourseCommentPageInfoVO selectPageList(@RequestBody @Valid CourseCommentQueryVO courseCommentQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", courseCommentQueryVO.toString());
        LOGGER.info(info);
        CourseCommentDO courseComment = BeanUtil.convert(courseCommentQueryVO, CourseCommentDO.class);

        Page<CourseCommentResponseVO> page = PageHelper.startPage(courseCommentQueryVO.getPageNum(), courseCommentQueryVO.getPageSize());
        List<CourseCommentDO> courseCommentList = courseCommentService.getList(courseComment);
        CourseCommentPageInfoVO courseCommentPageInfo = new CourseCommentPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), courseCommentPageInfo);
        courseCommentPageInfo.setPageNum(courseCommentQueryVO.getPageNum());
        List<CourseCommentResponseVO> voList = BeanUtil.convert(courseCommentList, CourseCommentResponseVO.class);
        courseCommentPageInfo.setList(voList);
        return courseCommentPageInfo;
    }

    /**
     * 已读
     * @param
     * @return 分页参数
     */
    @ApiOperation(value = "设置为已读")
    @PostMapping("read")
    public void read(@RequestBody List<Integer> ids){
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<CourseCommentDO> commentDOS  = new ArrayList<>(ids.size());
        CourseCommentDO commentDO;
        for(Integer id:ids){
            commentDO = new CourseCommentDO();
            commentDO.setId(id);
            commentDO.setUpdateTime(new Date());
            commentDO.setReadStatus(true);
            commentDO.setUpdateBy(ThreadUser.getUserName());
            commentDOS.add(commentDO);
        }
        courseCommentService.updateInBatch(commentDOS);
    }
    /**
     * 设置为精选
     * @param
     * @return 分页参数
     */
    @ApiOperation(value = "设置为精选")
    @PostMapping("selected")
    public void selected(@RequestBody List<Integer> ids){
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        courseCommentService.selectedComments(ids);
    }


}
